<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style type="text/css">
        .class-title {
            margin-top: 10px;
            margin-left: 10px;
            margin-right: 10px;
            font-size: 20px;
            font-weight: bold;
            color: #00BFFF;
        }

        .class-hr {
            margin-top: 5px;
            height: 1px;
            border: none;
            border-top: 1px solid #DCDCDC;
        }

        .class-seqno {
            margin-top: 3px;
            margin-left: 10px;
            margin-right: 25px;
            font-size: 10px;
            color: #808080;
            font-weight: bold;
            text-align: right;
        }

        .class-table {
            width: 100%;
            text-align: left;
            color: black;
            font-size: 13px;
            margin-left: 2%;
            width: 100%;
            table-layout: fixed;
        }

        .class-td {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .lw {
            background-color: #DCE9F2;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
            font-weight: bold;
        }

        .lw1 {
            background-color: #f0f5f5;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
        }

        /* .table {
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
            margin-bottom: 15px;
            width: 100%;
            border-collapse: collapse;
        }

        .table td {
            height: 40px;
            text-align: left;
            border: 0px solid #CBD6DE;
            padding: 0;
            margin: 0;
        } */

        .aui-searchbar-input {
            width: 95% !important;
        }
    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            发货扫码绑定

        </div>
    </header>
    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item" style="background-color: rgb(187, 185, 185);">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货计划号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="invoiceplancode" value="" disabled="true" />
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">产品条码</div>
                    <input type="text" class="aui-input" placeholder="请扫描产品条码" style="width: 80%;font-size: 16px;"
                        id="codeBar" onclick="getIdSelector();" />
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="padding-right:10px;font-size: 1.6rem"
                        onclick="doScan();"></i>
                </div>
            </li>

            <div class="aui-btn aui-btn-info aui-btn-block"><input type="text" class="aui-input"
                    style="width: 80%;font-size: 16px;" id="message" /></div>
            <div style="background-color:white;width: 100%;margin-top:2%;margin-bottom:5px;">
                <table class="table" style='width:100%;' id="table">
                    <thead>
                        <tr
                            style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 8px;font-weight: bold;">
                            <td algin='center'>物料编码</td>
                            <td algin='center'>规格</td>
                            <td algin='center'>计划数量</td>
                            <td algin='center'>绑定数量</td>
                        </tr>
                    </thead>
                    <tbody id="tbodyHtml">
                    </tbody>
                </table>
            </div>
        </ul>

        <ul class="ui-selectee" id="tbodyHtmlS" style="margin-bottom:5px;">
        </ul>

        <div style="width:100%;margin-top:18%;"></div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="beforeSave()" id="submitButton">确认发货</div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script>
    //光标所在id
    var idSelector = "";
    var workCenterGid = ""; //仓库GID
    var workCenterCode = ""; //仓库编码
    var workCenterName = ""; //仓库名称
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称

    var invoiceplancode = "";
    var subBussinessType = "";
    var mrlCode = "";
    var invoiceinfoid="";
    var title = "";
    var arrSave = []

    apiready = function () {
        invoiceplancode = api.pageParam.invoiceplancode;
        subBussinessType = api.pageParam.subBussinessType;
        invoiceinfoid=api.pageParam.invoiceinfoid;
        title = api.pageParam.titleText;
        $("#invoiceplancode").val(invoiceplancode);
        doSerchInvoiceInfo(invoiceplancode)
        if (title != undefined && title != null && title != "") {
            document.getElementById("middle").innerHTML = title + "扫码绑定";
        }
        idSelector = "codeBar";
        $("#codeBar").focus();

        //获取发货单已经绑定的明细
        getDetailList();
    }

    function doSerchInvoiceInfo(invoiceplancode) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSerchInvoiceInfo";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                invoiceplancode: invoiceplancode,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='drawing'>" + list[i].drawing + "</td>";
                        html += "<td name='planQty'>" + list[i].planQty + "</td>";
                        html += "<td name='bdQty'>" + list[i].bdQty + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }


    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    function setTable(list) {
        var html = "";
        for (var i = 0; i < list.length; i++) {
            html += "<li class='sortable ui-selectee ui-sortable'>";
            html += "<ul class='aui-list aui-media-list ui-selectee'>";
            html += "<li class='aui-list-item  ui-selectee'>";
            html += "<div class='aui-media-list-item-inner ui-selectee'>";
            html += "<div class='aui-list-item-inner ui-selectee'>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>产品条码：</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].scanCode + "</div>";
            html += "</div>";

            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料名称：</div>";
            if(list[i].mrlName===null){
                html += "<div class='aui-list-item-inner ui-selectee'>" + '' + "</div>";
            }else{
                html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrlName + "</div>";
            }
            html += "</div>";

            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>规格：</div>";
            if(list[i].drawing===null){
                html += "<div class='aui-list-item-inner ui-selectee'>" + '' + "</div>";
            }else{
                html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].drawing + "</div>";
            }
            html += "</div>";

            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>批次：</div>";
            if(list[i].drawing===null){
                html += "<div class='aui-list-item-inner ui-selectee'>" + '' + "</div>";
            }else{
                html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].lotCode + "</div>";
            }
            html += "</div>";


            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>操作类型：</div>";
            if(list[i].ext04===null){
                html += "<div class='aui-list-item-inner ui-selectee'>" + '' + "</div>";
            }else{
                html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].ext04 + "</div>";
            }
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;' >";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>操作原因：</div>";
            if(list[i].ext05===null){
                html += "<div class='aui-list-item-inner ui-selectee'>" + '' + "</div>";
            }else{
                html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].ext05 + "</div>";
            }
            html += "</div>";

            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'></div>";
            html += "<div class='aui-list-item-inner ui-selectee'></div>";
            html += "<div class='aui-list-item-right aui-btn ui-selectee' style='background:red;color:#f5f5f5;' onclick='delDetailSingle(\"" + list[i].sysid + "\",\"" + list[i].scanCode + "\");'>解绑</div>";
            html += "</div>";
            html += "</div>";
            html += "</li>";
            html += "</ul>";
            html += "</li>";
        }
        $api.html($api.byId("tbodyHtmlS"), html);
    };

    //获取发货单已经绑定的明细
    function getDetailList() {

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoiceinfoDetailList";
        var data = {
            values: {
                invoiceinfoid:invoiceinfoid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    var actualQty = 0;
                    arrSave = [];
                    for (var i = 0; i < list.length; i++) {
                        var model = {
                            sysid: list[i].sysid,
                            ext04: list[i].ext04,
                            ext05: list[i].ext05,
                            scanCode: list[i].packageno, //产品条码
                            mrlName: list[i].mrlName, // 物料名称
                            drawing: list[i].drawing, // 规格
                            qty: list[i].qty, // 数量
                            lotCode: list[i].lotCode, // 批次
                        }
                        arrSave.push(model);
                    }
                    setTable(arrSave);
                    //失败
                } else if (myobj.code == "1") {
                    //清空表格数据
                    $api.html($api.byId("tbodyHtmlS"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：仓库
            if (idSelector == 'workCenterCode') {

                $("#workCenterCode").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector);

                //2:库位
            } else if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

                //扫描条码
            } else if (idSelector == 'codeBar') {

                $("#codeBar").val(ret);
                checkBarCode(ret)
            }
        });
    }



    //3；PDA自带扫描回车事件
    $("#codeBar").on('keypress', function (e) { //#keyword为input文本框

        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            if (sm != null && sm != "" && sm != undefined) {
                console.log(sm)
                $("#codeBar").blur();
                //调用条码解析+保存事件
                //saveData(sm);
                checkBarCode(sm);
            }
        }
    });


    function checkBarCode(tm) {
        mrlCode = "";
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/checkBarCodeNew";
        var data = {
            values: {
                invoiceplancode: invoiceplancode,
                barCode: tm,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == '0') {
                    mrlCode = myobj.mrlCode;
                    saveAuto();
                } else if (myobj.code == '1') {

                    $("#codeBar").val("");
                    $("#codeBar").select();
                    idSelector = "codeBar";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //自动保存
    function saveAuto() {
        var codeBar = $("#codeBar").val();

        //容器码非空校验
        if (codeBar == "" || codeBar == null || codeBar == undefined) {
            return;
        }

        save();
    }

    function save() {

        var codeBar = $("#codeBar").val();


        //容器码非空校验
        if (codeBar == "" || codeBar == null || codeBar == undefined) {
            api.toast({
                msg: "请扫描产品条码",
                duration: 1500
            });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoiceinfoDetailSaveNew";
        var data = {
            un: un,
            invoiceplancode: api.pageParam.invoiceplancode,
            workCenterCode: workCenterCode,
            workCellCode: workCellCode,
            subBussinessType: subBussinessType,
            barCode: codeBar,
        };
        $.ajax({
            type: 'post',
            async: false,
            url: url,
            data: data,
            dataType: 'json',
            success: function (ret) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");
                    //成功
                    if (myobj.code == '0') {
                        api.hideProgress();
                        api.toast({
                            msg: "保存成功",
                            duration: 3000
                        });

                        //清空表单
                        $("#codeBar").val("");
                        $("#codeBar").focus();
                        $("#codeBar").select();
                        idSelector = "codeBar";
                        doSerchInvoiceInfo(invoiceplancode)
                        getDetailList();

                    } else if (myobj.code == '1') {
                        $("#codeBar").val("");
                        api.hideProgress();
                        $("#codeBar").select();
                        idSelector = "codeBar";
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                    }
                } else {
                    $("#codeBar").val("");
                    $("#codeBar").select();
                    idSelector = "codeBar";
                    api.hideProgress();
                    alert("连接超时，请耐心等待");
                }
            }
        });
    }

    function saveAll() {
        const submitButton = document.getElementById('submitButton');

        submitButton.style.pointerEvents = "none"
        var ctBarRedis = []
        arrSave.forEach(item => {
            if(item.ext04 == null 
            || item.ext04 == '' 
            || item.ext04 == undefined 
            || item.ext04 == 'E'){
                ctBarRedis.push(item.rsBarcode);
            }
            
        });
        console.log(ctBarRedis)
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoicePlanSave";
        var data = {
            un: un,
            invoiceplancode: api.pageParam.invoiceplancode
            ,ctBarRedis:JSON.stringify(ctBarRedis)
        };
        $.ajax({
            type: 'post',
            async: false,
            url: url,
            data: data,
            dataType: 'json',
            success: function (ret) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");
                    //成功
                    if (myobj.code == '0') {
                        api.hideProgress();
                        submitButton.style.pointerEvents = "auto"
                        api.toast({
                            msg: "保存成功",
                            duration: 3000
                        });
                        idSelector = "codeBar";

                        closeWin();

                    } else if (myobj.code == '1') {
                        $("#codeBar").val("");
                        api.hideProgress();
                        submitButton.style.pointerEvents = "auto"
                        $("#codeBar").select();
                        idSelector = "codeBar";
                        getDetailList();
                        $("#message").val(myobj.msg);
                        // api.toast({
                        //     msg: myobj.msg,
                        //     duration: 3000
                        // });
                    }
                } else {
                    $("#codeBar").val("");
                    $("#codeBar").select();
                    idSelector = "codeBar";
                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"
                    alert("连接超时，请耐心等待");
                }
            }
        });
    }


    //删除单个明细
    function delDetailSingle(sysid, rsBarcode) {
        api.confirm({
            title: '提示',
            msg: '您确定要解绑容器条码为【' + rsBarcode + '】下的所有明细吗？',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            if (ret.buttonIndex == 1) {
                var un = localStorage.getItem('un');
                var data = {
                    values: {
                        un: un,
                        rsBarcode:rsBarcode,
                        invoiceinfoid: api.pageParam.invoiceinfoid,
                        // sysid: sysid,
                        sysid: '',
                        flag: "SINGLE" //ALL 删除所有明细 SINGLE 单个删除
                    }
                };
                delData(data);
            }
        });
    }

    function delData(data) {

        api.showProgress({
            title: '操作中...',
        });

        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoiceinfoDetaildeleteNew";

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "删除成功",
                        duration: 3000
                    });

                    //清空表单
                    $("#codeBar").val("");
                    getDetailList();

                    // 查询物料信息
                    doSerchInvoiceInfo(invoiceplancode);

                } else if (myobj.code == '1') {
                    $("#codeBar").val("");
                    api.hideProgress();
                    $("#codeBar").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }
    //返回按钮
    function closeWin() {
        api.closeWin({});

        api.execScript({
            name: "wmInvoiceInfo",
            script: 'refreshData()'
        });
    }

    function beforeSave() {
        api.confirm({
            title: '系统提示：',
            msg: '确认发货成功后无法继续绑定？\n是否确认？',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            if (ret.buttonIndex == 1) {
                saveAll()
            }
        });
    }
</script>

</html>